package com.roc.steelend.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roc.steelend.controller.vo.account.*;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.controller.vo.returnformat.ReturnMsgForTable;
import com.roc.steelend.mapper.*;
import com.roc.steelend.pojo.*;
import com.roc.steelend.service.AccountService;
import com.roc.steelend.util.BuyOrderByAccountComparator;
import com.roc.steelend.util.KaipingOrderByAccountComparator;
import com.roc.steelend.util.LshUtil;
import com.roc.steelend.util.SaleOrderByAccountComparator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.security.auth.Subject;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.*;

@Service
@Transactional(rollbackFor =  Exception.class)
public class AccountServiceImpl  implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private CompanyMoneyMapper companyMoneyMapper;

    @Autowired
    private SuppliersMapper suppliersMapper;

    @Autowired
    private ClientMapper clientMapper;
    @Autowired
    private KaipingGuyMapper kaipingGuyMapper;
    @Autowired
    private SaleOrderMapper saleOrderMapper;
    @Autowired
    private BuyOrderMapper buyOrderMapper;
    @Autowired
    private  KaipingOrderMapper kaipingOrderMapper;
    @Autowired
    private WriteOffBuyDetailMapper writeOffBuyDetailMapper;
    @Autowired
    private WriteOffSaleDetailMapper writeOffSaleDetailMapper;
    @Autowired
    private WriteOffKaipingDetailMapper writeOffKaipingDetailMapper;
    @Override
    public Boolean addAccountCommon(AccountCommonReception accountCommonReception,Integer employeeId) {
        Account account = new Account();
        account.setAccountId(LshUtil.getAccountCommonLsh());
        account.setEmployeeId(employeeId);
        account.setPayTakeId(accountCommonReception.getPayOrTakeWay());
        account.setSmallAccountId(accountCommonReception.getSmallAccountId());
        account.setOperateTime(new Timestamp(new Date().getTime()));
        account.setAccountTime(accountCommonReception.getTime());
        account.setMoney(accountCommonReception.getGiveMoney());
        account.setWho(accountCommonReception.getDuiXiang());
        account.setRemark(accountCommonReception.getRemark());
        account.setWay(1);
        int insert = accountMapper.insert(account);
        QueryWrapper<CompanyMoney> wrapper = new QueryWrapper<>();
        wrapper.eq("pay_take_id",accountCommonReception.getPayOrTakeWay());
        List<CompanyMoney> companyMonies = companyMoneyMapper.selectList(wrapper);
        CompanyMoney companyMoney = companyMonies.get(0);
        BigDecimal money = companyMoney.getMoney();
        Boolean temp=false;
        if (accountCommonReception.getType()==1){
//                支出
            BigDecimal subtract = money.subtract(accountCommonReception.getGiveMoney());
            companyMoney.setMoney(subtract);
            int updateById = companyMoneyMapper.updateById(companyMoney);
            if (updateById==1){
                temp=true;
            }
        }else {
//            收入
            BigDecimal add = money.add(accountCommonReception.getGiveMoney());
            companyMoney.setMoney(add);
            int updateById = companyMoneyMapper.updateById(companyMoney);
            if (updateById==1){
                temp=true;
            }
        }


        if (temp&&insert==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public ReturnMsg postAccountSpecialBuy(AccountSpecialBuyReception accountSpecialBuyReception, Integer employeeId) {
//            存入账务数据
        ReturnMsg msg = new ReturnMsg();
        Account account = new Account();
        BigInteger accountSpecialLsh = LshUtil.getAccountSpecialLsh();
        account.setAccountId(accountSpecialLsh);
        account.setEmployeeId(employeeId);
        account.setPayTakeId(accountSpecialBuyReception.getPayOrTakeWay());
        account.setSmallAccountId(2);
        account.setOperateTime(new Timestamp(new Date().getTime()));
        account.setAccountTime(accountSpecialBuyReception.getPayTime());
        account.setMoney(accountSpecialBuyReception.getPayMoney());
        account.setWho(accountSpecialBuyReception.getSupplierName());
        account.setRemark(accountSpecialBuyReception.getRemark());
        account.setWay(2);
        int accountInsert = accountMapper.insert(account);

        if (accountInsert==0){
            msg.setCode(0);
            msg.setMsg("账务存入操作失败,请重试");
            return msg;
        }

//        操作公司的总账务表
        QueryWrapper<CompanyMoney> wrapper = new QueryWrapper<>();
        wrapper.eq("pay_take_id",accountSpecialBuyReception.getPayOrTakeWay());
        List<CompanyMoney> companyMonies = companyMoneyMapper.selectList(wrapper);
        CompanyMoney companyMoney = companyMonies.get(0);
        BigDecimal money = companyMoney.getMoney();
        companyMoney.setMoney(money.subtract(accountSpecialBuyReception.getPayMoney()));
        int companyInsert = companyMoneyMapper.updateById(companyMoney);


//        不抹零的情况
        if (!accountSpecialBuyReception.getIsOver()){
//            操作供应商表
            QueryWrapper<Suppliers> supplerWrapper= new QueryWrapper<>();
            supplerWrapper.eq("supplier_id",accountSpecialBuyReception.getSupplierId());
            List<Suppliers> suppliersList = suppliersMapper.selectList(supplerWrapper);
            Suppliers suppliers = suppliersList.get(0);
            BigDecimal debt = suppliers.getDebt();
            suppliers.setDebt(debt.subtract(accountSpecialBuyReception.getPayMoney()));
            suppliersMapper.updateById(suppliers);
//            操作销账细节表和采购订单表
            List<AccountSpecialBuyOrderIdDetail> buyOrderIdList = accountSpecialBuyReception.getBuyOrderIdList();
            Collections.sort(buyOrderIdList,new BuyOrderByAccountComparator());
            if (Objects.equals(accountSpecialBuyReception.getShouldGiveMoney(), accountSpecialBuyReception.getPayMoney())){
//                便捷扣款，给的钱等于应给的钱
                for (int i = 0; i < buyOrderIdList.size(); i++) {
//                    操作采购订单
                    BigInteger buyOrderId = buyOrderIdList.get(i).getBuyOrderId();
                    QueryWrapper<BuyOrder> buyOrderQueryWrapper = new QueryWrapper<>();
                    buyOrderQueryWrapper.eq("buy_order_id",buyOrderId);
                    BuyOrder buyOrder = buyOrderMapper.selectList(buyOrderQueryWrapper).get(0);
                    BigDecimal woffMsg = buyOrder.getSumMoney().subtract(buyOrder.getGiven());
                    buyOrder.setGiven(buyOrder.getSumMoney());
                    buyOrder.setPaymentIsSettled(1);
                    buyOrder.setIsWriteOff(1);
                    buyOrderMapper.updateById(buyOrder);
//                    操作销账细节
                    WriteOffBuyDetail writeOffBuyDetail = new WriteOffBuyDetail();
                    writeOffBuyDetail.setAccountId(accountSpecialLsh);
                    writeOffBuyDetail.setBuyOrderId(buyOrderId);
                    writeOffBuyDetail.setGetMoney(woffMsg);
                    writeOffBuyDetail.setMoLingTou(0);
                    writeOffBuyDetailMapper.insert(writeOffBuyDetail);

                }
            }else {
//                  普通扣款，给的钱不等于应给的钱
                BigDecimal payMoney = accountSpecialBuyReception.getPayMoney(); //给的钱
                for (int i = 0; i <buyOrderIdList.size() ; i++) {
                    BigInteger buyOrderId = buyOrderIdList.get(i).getBuyOrderId();
                    QueryWrapper<BuyOrder> buyOrderQueryWrapper = new QueryWrapper<>();
                    buyOrderQueryWrapper.eq("buy_order_id",buyOrderId);
                    BuyOrder buyOrder = buyOrderMapper.selectList(buyOrderQueryWrapper).get(0);
                    BigDecimal given = buyOrder.getGiven();
                    BigDecimal sumMoney = buyOrder.getSumMoney();
                    BigDecimal shouldGiveMoney = sumMoney.subtract(given);
                    if (shouldGiveMoney.compareTo(payMoney)==0){//a=b   给的钱等于需要给的钱  return
                        buyOrder.setGiven(given.add(payMoney));
                        buyOrder.setPaymentIsSettled(1);
                        buyOrder.setIsWriteOff(1);
                        buyOrderMapper.updateById(buyOrder);
                        WriteOffBuyDetail writeOffBuyDetail = new WriteOffBuyDetail();
                        writeOffBuyDetail.setAccountId(accountSpecialLsh);
                        writeOffBuyDetail.setBuyOrderId(buyOrderId);
                        writeOffBuyDetail.setGetMoney(payMoney);
                        writeOffBuyDetail.setMoLingTou(0);
                        writeOffBuyDetailMapper.insert(writeOffBuyDetail);
                        break;
                    }else if (shouldGiveMoney.compareTo(payMoney)==-1){//a<b 需要给的钱小于给的钱
                        payMoney=payMoney.subtract(shouldGiveMoney);
                        buyOrder.setGiven(given.add(shouldGiveMoney));
                        buyOrder.setPaymentIsSettled(1);
                        buyOrder.setIsWriteOff(1);
                        buyOrderMapper.updateById(buyOrder);
                        WriteOffBuyDetail writeOffBuyDetail = new WriteOffBuyDetail();
                        writeOffBuyDetail.setAccountId(accountSpecialLsh);
                        writeOffBuyDetail.setBuyOrderId(buyOrderId);
                        writeOffBuyDetail.setGetMoney(shouldGiveMoney);
                        writeOffBuyDetail.setMoLingTou(0);
                        writeOffBuyDetailMapper.insert(writeOffBuyDetail);
                        continue;
                    }else if (shouldGiveMoney.compareTo(payMoney)==1){//a>b 需要给的钱大于给的钱 return
                        buyOrder.setGiven(given.add(payMoney));
                        buyOrder.setIsWriteOff(1);
                        buyOrderMapper.updateById(buyOrder);
                        WriteOffBuyDetail writeOffBuyDetail = new WriteOffBuyDetail();
                        writeOffBuyDetail.setAccountId(accountSpecialLsh);
                        writeOffBuyDetail.setBuyOrderId(buyOrderId);
                        writeOffBuyDetail.setGetMoney(payMoney);
                        writeOffBuyDetail.setMoLingTou(0);
                        writeOffBuyDetailMapper.insert(writeOffBuyDetail);
                        break;
                    }

                }
            }

        }else {
//            抹零的情况
            QueryWrapper<Suppliers> supplerWrapper= new QueryWrapper<>();
            supplerWrapper.eq("supplier_id",accountSpecialBuyReception.getSupplierId());
            List<Suppliers> suppliersList = suppliersMapper.selectList(supplerWrapper);
            Suppliers suppliers = suppliersList.get(0);
            BigDecimal debt = suppliers.getDebt();
            suppliers.setDebt(debt.subtract(accountSpecialBuyReception.getShouldGiveMoney()));
            suppliersMapper.updateById(suppliers);
            List<AccountSpecialBuyOrderIdDetail> buyOrderIdList = accountSpecialBuyReception.getBuyOrderIdList();
            Collections.sort(buyOrderIdList,new BuyOrderByAccountComparator());
            for (int i = 0; i < buyOrderIdList.size(); i++) {
//                    操作采购订单
                BigInteger buyOrderId = buyOrderIdList.get(i).getBuyOrderId();
                QueryWrapper<BuyOrder> buyOrderQueryWrapper = new QueryWrapper<>();
                buyOrderQueryWrapper.eq("buy_order_id",buyOrderId);
                BuyOrder buyOrder = buyOrderMapper.selectList(buyOrderQueryWrapper).get(0);
                BigDecimal temp = buyOrder.getSumMoney().subtract(buyOrder.getGiven());
                buyOrder.setGiven(buyOrder.getSumMoney());
                buyOrder.setPaymentIsSettled(1);
                buyOrder.setIsWriteOff(1);
                buyOrder.setMoLingTou(1);
                buyOrderMapper.updateById(buyOrder);
//                    操作销账细节
                WriteOffBuyDetail writeOffBuyDetail = new WriteOffBuyDetail();
                writeOffBuyDetail.setAccountId(accountSpecialLsh);
                writeOffBuyDetail.setBuyOrderId(buyOrderId);
                writeOffBuyDetail.setGetMoney(temp);
                writeOffBuyDetail.setMoLingTou(1);
                writeOffBuyDetailMapper.insert(writeOffBuyDetail);
            }



        }

        msg.setCode(1);
        msg.setMsg("采购付款账务操作成功");


        return msg;
    }

    @Override
    public ReturnMsg postAccountSpecialSale(AccountSpecialSaleReception accountSpecialSaleReception, Integer employeeId) {
//            存入账务数据
        ReturnMsg msg = new ReturnMsg();
        Account account = new Account();
        BigInteger accountSpecialLsh = LshUtil.getAccountSpecialLsh();
        account.setAccountId(accountSpecialLsh);
        account.setEmployeeId(employeeId);
        account.setPayTakeId(accountSpecialSaleReception.getPayOrTakeWay());
        account.setSmallAccountId(1);
        account.setOperateTime(new Timestamp(new Date().getTime()));
        account.setAccountTime(accountSpecialSaleReception.getPayTime());
        account.setMoney(accountSpecialSaleReception.getPayMoney());
        account.setWho(accountSpecialSaleReception.getClientName());
        account.setRemark(accountSpecialSaleReception.getRemark());
        account.setWay(2);
        int accountInsert = accountMapper.insert(account);

        if (accountInsert==0){
            msg.setCode(0);
            msg.setMsg("账务存入操作失败,请重试");
            return msg;
        }

//        操作公司的总账务表
        QueryWrapper<CompanyMoney> wrapper = new QueryWrapper<>();
        wrapper.eq("pay_take_id",accountSpecialSaleReception.getPayOrTakeWay());
        List<CompanyMoney> companyMonies = companyMoneyMapper.selectList(wrapper);
        CompanyMoney companyMoney = companyMonies.get(0);
        BigDecimal money = companyMoney.getMoney();
        companyMoney.setMoney(money.add(accountSpecialSaleReception.getPayMoney()));
        int companyInsert = companyMoneyMapper.updateById(companyMoney);


        //        不抹零的情况
        if (!accountSpecialSaleReception.getIsOver()){
//            操作供应商表
            QueryWrapper<Client> clientWRapper= new QueryWrapper<>();
            clientWRapper.eq("client_id",accountSpecialSaleReception.getClientId());
            List<Client> clientList = clientMapper.selectList(clientWRapper);
            Client client = clientList.get(0);
            BigDecimal debt = client.getDebt();
            client.setDebt(debt.subtract(accountSpecialSaleReception.getPayMoney()));
            clientMapper.updateById(client);
//            操作销账细节表和采购订单表
            List<AccountSpecialSaleOrderIdDetail> saleOrderIdList = accountSpecialSaleReception.getSaleOrderIdList();
            Collections.sort(saleOrderIdList,new SaleOrderByAccountComparator());
            if (Objects.equals(accountSpecialSaleReception.getShouldGiveMoney(), accountSpecialSaleReception.getPayMoney())){
//                便捷扣款，给的钱等于应给的钱
                for (int i = 0; i < saleOrderIdList.size(); i++) {
//                    操作采购订单
                    BigInteger saleOrderId = saleOrderIdList.get(i).getSaleOrderId();
                    QueryWrapper<SaleOrder> saleOrderQueryWrapper = new QueryWrapper<>();
                    saleOrderQueryWrapper.eq("sale_order_id",saleOrderId);
                    SaleOrder saleOrder = saleOrderMapper.selectList(saleOrderQueryWrapper).get(0);
                    BigDecimal woffMsg = saleOrder.getSumMoney().subtract(saleOrder.getGiven());
                    saleOrder.setGiven(saleOrder.getSumMoney());
                    saleOrder.setPaymentIsSettled(1);
                    saleOrder.setIsWriteOff(1);
                    saleOrderMapper.updateById(saleOrder);
//                    操作销账细节
                    WriteOffSaleDetail writeOffSaleDetail = new WriteOffSaleDetail();
                    writeOffSaleDetail.setAccountId(accountSpecialLsh);
                    writeOffSaleDetail.setSaleOrderId(saleOrderId);
                    writeOffSaleDetail.setGetMoney(woffMsg);
                    writeOffSaleDetail.setMoLingTou(0);
                    writeOffSaleDetailMapper.insert(writeOffSaleDetail);

                }
            }else {
//                  普通扣款，给的钱不等于应给的钱
                BigDecimal payMoney = accountSpecialSaleReception.getPayMoney(); //给的钱
                for (int i = 0; i <saleOrderIdList.size() ; i++) {
                    BigInteger saleOrderId = saleOrderIdList.get(i).getSaleOrderId();
                    QueryWrapper<SaleOrder> saleOrderQueryWrapper = new QueryWrapper<>();
                    saleOrderQueryWrapper.eq("sale_order_id",saleOrderId);
                    SaleOrder saleOrder = saleOrderMapper.selectList(saleOrderQueryWrapper).get(0);
                    BigDecimal given = saleOrder.getGiven();
                    BigDecimal sumMoney = saleOrder.getSumMoney();
                    BigDecimal shouldGiveMoney = sumMoney.subtract(given);
                    if (shouldGiveMoney.compareTo(payMoney)==0){//a=b   给的钱等于需要给的钱  return
                        saleOrder.setGiven(given.add(payMoney));
                        saleOrder.setPaymentIsSettled(1);
                        saleOrder.setIsWriteOff(1);
                        saleOrderMapper.updateById(saleOrder);
                        WriteOffSaleDetail writeOffSaleDetail = new WriteOffSaleDetail();
                        writeOffSaleDetail.setAccountId(accountSpecialLsh);
                        writeOffSaleDetail.setSaleOrderId(saleOrderId);
                        writeOffSaleDetail.setGetMoney(payMoney);
                        writeOffSaleDetail.setMoLingTou(0);
                        writeOffSaleDetailMapper.insert(writeOffSaleDetail);
                        break;
                    }else if (shouldGiveMoney.compareTo(payMoney)==-1){//a<b 需要给的钱小于给的钱
                        payMoney=payMoney.subtract(shouldGiveMoney);
                        saleOrder.setGiven(given.add(shouldGiveMoney));
                        saleOrder.setPaymentIsSettled(1);
                        saleOrder.setIsWriteOff(1);
                        saleOrderMapper.updateById(saleOrder);
                        WriteOffSaleDetail writeOffSaleDetail = new WriteOffSaleDetail();
                        writeOffSaleDetail.setAccountId(accountSpecialLsh);
                        writeOffSaleDetail.setSaleOrderId(saleOrderId);
                        writeOffSaleDetail.setGetMoney(shouldGiveMoney);
                        writeOffSaleDetail.setMoLingTou(0);
                        writeOffSaleDetailMapper.insert(writeOffSaleDetail);
                        continue;
                    }else if (shouldGiveMoney.compareTo(payMoney)==1){//a>b 需要给的钱大于给的钱 return
                        saleOrder.setGiven(given.add(payMoney));
                        saleOrder.setIsWriteOff(1);
                        saleOrderMapper.updateById(saleOrder);
                        WriteOffSaleDetail writeOffSaleDetail = new WriteOffSaleDetail();
                        writeOffSaleDetail.setAccountId(accountSpecialLsh);
                        writeOffSaleDetail.setSaleOrderId(saleOrderId);
                        writeOffSaleDetail.setGetMoney(payMoney);
                        writeOffSaleDetail.setMoLingTou(0);
                        writeOffSaleDetailMapper.insert(writeOffSaleDetail);
                        break;
                    }

                }
            }

        }else {
//            抹零的情况
            QueryWrapper<Client> clientWrapper= new QueryWrapper<>();
            clientWrapper.eq("client_id",accountSpecialSaleReception.getClientId());
            List<Client> clientList = clientMapper.selectList(clientWrapper);
            Client client = clientList.get(0);
            BigDecimal debt = client.getDebt();
            client.setDebt(debt.subtract(accountSpecialSaleReception.getShouldGiveMoney()));
            clientMapper.updateById(client);
            List<AccountSpecialSaleOrderIdDetail> saleOrderIdList = accountSpecialSaleReception.getSaleOrderIdList();
            Collections.sort(saleOrderIdList,new SaleOrderByAccountComparator());
            for (int i = 0; i < saleOrderIdList.size(); i++) {
//                    操作采购订单
                BigInteger saleOrderId = saleOrderIdList.get(i).getSaleOrderId();
                QueryWrapper<SaleOrder> saleOrderQueryWrapper = new QueryWrapper<>();
                saleOrderQueryWrapper.eq("sale_order_id",saleOrderId);
                SaleOrder saleOrder = saleOrderMapper.selectList(saleOrderQueryWrapper).get(0);
                BigDecimal temp = saleOrder.getSumMoney().subtract(saleOrder.getGiven());
                saleOrder.setGiven(saleOrder.getSumMoney());
                saleOrder.setPaymentIsSettled(1);
                saleOrder.setIsWriteOff(1);
                saleOrder.setMoLingTou(1);
                saleOrderMapper.updateById(saleOrder);
//                    操作销账细节
                WriteOffSaleDetail writeOffSaleDetail = new WriteOffSaleDetail();
                writeOffSaleDetail.setAccountId(accountSpecialLsh);
                writeOffSaleDetail.setSaleOrderId(saleOrderId);
                writeOffSaleDetail.setGetMoney(temp);
                writeOffSaleDetail.setMoLingTou(1);
                writeOffSaleDetailMapper.insert(writeOffSaleDetail);
            }



        }
        msg.setCode(1);
        msg.setMsg("销售收款账务操作成功");
        return msg;
    }

    @Override
    public ReturnMsg postAccountSpecialKaiping(AccountSpecialKaipingReception accountSpecialKaipingReception, Integer employeeId) {
        //            存入账务数据
        ReturnMsg msg = new ReturnMsg();
        Account account = new Account();
        BigInteger accountSpecialLsh = LshUtil.getAccountSpecialLsh();
        account.setAccountId(accountSpecialLsh);
        account.setEmployeeId(employeeId);
        account.setPayTakeId(accountSpecialKaipingReception.getPayOrTakeWay());
        account.setSmallAccountId(8);
        account.setOperateTime(new Timestamp(new Date().getTime()));
        account.setAccountTime(accountSpecialKaipingReception.getPayTime());
        account.setMoney(accountSpecialKaipingReception.getPayMoney());
        account.setWho(accountSpecialKaipingReception.getKaipingguyName());
        account.setRemark(accountSpecialKaipingReception.getRemark());
        account.setWay(2);
        int accountInsert = accountMapper.insert(account);

        if (accountInsert==0){
            msg.setCode(0);
            msg.setMsg("账务存入操作失败,请重试");
            return msg;
        }

//        操作公司的总账务表
        QueryWrapper<CompanyMoney> wrapper = new QueryWrapper<>();
        wrapper.eq("pay_take_id",accountSpecialKaipingReception.getPayOrTakeWay());
        List<CompanyMoney> companyMonies = companyMoneyMapper.selectList(wrapper);
        CompanyMoney companyMoney = companyMonies.get(0);
        BigDecimal money = companyMoney.getMoney();
        companyMoney.setMoney(money.subtract(accountSpecialKaipingReception.getPayMoney()));
        int companyInsert = companyMoneyMapper.updateById(companyMoney);


        //        不抹零的情况
        if (!accountSpecialKaipingReception.getIsOver()){
//            操作供应商表
            QueryWrapper<KaipingGuy> kaipingWrapper= new QueryWrapper<>();
            kaipingWrapper.eq("kaipingguy_id",accountSpecialKaipingReception.getKaipingguyId());
            KaipingGuy kaipingGuy =     kaipingGuyMapper.selectList(kaipingWrapper).get(0);

            BigDecimal debt = kaipingGuy.getDebt();
            kaipingGuy.setDebt(debt.subtract(accountSpecialKaipingReception.getPayMoney()));
            kaipingGuyMapper.updateById(kaipingGuy);
//            操作销账细节表和采购订单表
            List<AccountSpecialKaipingOrderIdDetail> kaipingOrderIdList = accountSpecialKaipingReception.getKaipingOrderIdList();
            Collections.sort(kaipingOrderIdList,new KaipingOrderByAccountComparator());
            if (Objects.equals(accountSpecialKaipingReception.getShouldGiveMoney(), accountSpecialKaipingReception.getPayMoney())){
//                便捷扣款，给的钱等于应给的钱
                for (int i = 0; i < kaipingOrderIdList.size(); i++) {
//                    操作采购订单
                    BigInteger kaipingOrderId = kaipingOrderIdList.get(i).getKaipingOrderId();
                    QueryWrapper<KaipingOrder> kaipingOrderQueryWrapper = new QueryWrapper<>();
                    kaipingOrderQueryWrapper.eq("kaiping_order_id",kaipingOrderId);
                    KaipingOrder kaipingOrder = kaipingOrderMapper.selectList(kaipingOrderQueryWrapper).get(0);
                    BigDecimal woffMsg = kaipingOrder.getMoney().subtract(kaipingOrder.getGiven());
                    kaipingOrder.setGiven(kaipingOrder.getMoney());
                    kaipingOrder.setPaymentIsSettled(1);
                    kaipingOrder.setIsWriteOff(1);
                    kaipingOrderMapper.updateById(kaipingOrder);
//                    操作销账细节
                    WriteOffKaipingDetail writeOffKaipingDetail = new WriteOffKaipingDetail();
                    writeOffKaipingDetail.setAccountId(accountSpecialLsh);
                    writeOffKaipingDetail.setKaipingOrderId(kaipingOrderId);
                    writeOffKaipingDetail.setGetMoney(woffMsg);
                    writeOffKaipingDetail.setMoLingTou(0);
                    writeOffKaipingDetailMapper.insert(writeOffKaipingDetail);

                }
            }else {
//                  普通扣款，给的钱不等于应给的钱
                BigDecimal payMoney = accountSpecialKaipingReception.getPayMoney(); //给的钱
                for (int i = 0; i <kaipingOrderIdList.size() ; i++) {
                    BigInteger kaipingOrderId = kaipingOrderIdList.get(i).getKaipingOrderId();
                    QueryWrapper<KaipingOrder> kaipingOrderQueryWrapper = new QueryWrapper<>();
                    kaipingOrderQueryWrapper.eq("kaiping_order_id",kaipingOrderId);
                    KaipingOrder kaipingOrder = kaipingOrderMapper.selectList(kaipingOrderQueryWrapper).get(0);
                    BigDecimal given = kaipingOrder.getGiven();
                    BigDecimal sumMoney = kaipingOrder.getMoney();
                    BigDecimal shouldGiveMoney = sumMoney.subtract(given);
                    if (shouldGiveMoney.compareTo(payMoney)==0){//a=b   给的钱等于需要给的钱  return
                        kaipingOrder.setGiven(given.add(payMoney));
                        kaipingOrder.setPaymentIsSettled(1);
                        kaipingOrder.setIsWriteOff(1);
                        kaipingOrderMapper.updateById(kaipingOrder);
                        WriteOffKaipingDetail writeOffKaipingDetail = new WriteOffKaipingDetail();
                        writeOffKaipingDetail.setAccountId(accountSpecialLsh);
                        writeOffKaipingDetail.setKaipingOrderId(kaipingOrderId);
                        writeOffKaipingDetail.setGetMoney(payMoney);
                        writeOffKaipingDetail.setMoLingTou(0);
                        writeOffKaipingDetailMapper.insert(writeOffKaipingDetail);
                        break;
                    }else if (shouldGiveMoney.compareTo(payMoney)==-1){//a<b 需要给的钱小于给的钱
                        payMoney=payMoney.subtract(shouldGiveMoney);
                        kaipingOrder.setGiven(given.add(shouldGiveMoney));
                        kaipingOrder.setPaymentIsSettled(1);
                        kaipingOrder.setIsWriteOff(1);
                        kaipingOrderMapper.updateById(kaipingOrder);
                        WriteOffKaipingDetail writeOffKaipingDetail = new WriteOffKaipingDetail();
                        writeOffKaipingDetail.setAccountId(accountSpecialLsh);
                        writeOffKaipingDetail.setKaipingOrderId(kaipingOrderId);
                        writeOffKaipingDetail.setGetMoney(shouldGiveMoney);
                        writeOffKaipingDetail.setMoLingTou(0);
                        writeOffKaipingDetailMapper.insert(writeOffKaipingDetail);
                        continue;
                    }else if (shouldGiveMoney.compareTo(payMoney)==1){//a>b 需要给的钱大于给的钱 return
                        kaipingOrder.setGiven(given.add(payMoney));
                        kaipingOrder.setIsWriteOff(1);
                        kaipingOrderMapper.updateById(kaipingOrder);
                        WriteOffKaipingDetail writeOffKaipingDetail = new WriteOffKaipingDetail();

                        writeOffKaipingDetail.setAccountId(accountSpecialLsh);
                        writeOffKaipingDetail.setKaipingOrderId(kaipingOrderId);
                        writeOffKaipingDetail.setGetMoney(payMoney);
                        writeOffKaipingDetail.setMoLingTou(0);
                        writeOffKaipingDetailMapper.insert(writeOffKaipingDetail);
                        break;
                    }

                }
            }

        }else {
//            抹零的情况
            QueryWrapper<KaipingGuy> kaipingGuyWrapper= new QueryWrapper<>();
            kaipingGuyWrapper.eq("kaipingguy_id",accountSpecialKaipingReception.getKaipingguyId());
            KaipingGuy kaipingGuy = kaipingGuyMapper.selectList(kaipingGuyWrapper).get(0);

            BigDecimal debt = kaipingGuy.getDebt();
            kaipingGuy.setDebt(debt.subtract(accountSpecialKaipingReception.getShouldGiveMoney()));
            kaipingGuyMapper.updateById(kaipingGuy);
            List<AccountSpecialKaipingOrderIdDetail> kaipingOrderIdList = accountSpecialKaipingReception.getKaipingOrderIdList();
            Collections.sort(kaipingOrderIdList,new KaipingOrderByAccountComparator());
            for (int i = 0; i < kaipingOrderIdList.size(); i++) {
//                    操作采购订单
                BigInteger kaipingOrderId = kaipingOrderIdList.get(i).getKaipingOrderId();
                QueryWrapper<KaipingOrder> kaipingOrderQueryWrapper = new QueryWrapper<>();
                kaipingOrderQueryWrapper.eq("kaiping_order_id",kaipingOrderId);
                KaipingOrder kaipingOrder = kaipingOrderMapper.selectList(kaipingOrderQueryWrapper).get(0);
                BigDecimal temp = kaipingOrder.getMoney().subtract(kaipingOrder.getGiven());
                kaipingOrder.setGiven(kaipingOrder.getMoney());
                kaipingOrder.setPaymentIsSettled(1);
                kaipingOrder.setIsWriteOff(1);
                kaipingOrder.setMoLingTou(1);
                kaipingOrderMapper.updateById(kaipingOrder);
//                    操作销账细节
                WriteOffKaipingDetail writeOffKaipingDetail = new WriteOffKaipingDetail();

                writeOffKaipingDetail.setAccountId(accountSpecialLsh);
                writeOffKaipingDetail.setKaipingOrderId(kaipingOrderId);
                writeOffKaipingDetail.setGetMoney(temp);
                writeOffKaipingDetail.setMoLingTou(1);
                writeOffKaipingDetailMapper.insert(writeOffKaipingDetail);
            }



        }
        msg.setCode(1);
        msg.setMsg("开平付款账务操作成功");
        return msg;
    }

    @Override
    public List<Account> selectAccountData(FindAccountReception findAccountReception) {
        Integer now = findAccountReception.getCurrentPage();
        Integer size = findAccountReception.getPageSize();
        Integer way = findAccountReception.getWay();
        Integer payOrTakeWay = findAccountReception.getPayOrTakeWay();
        Integer bigAccountId = findAccountReception.getBigAccountId();
        Integer smallAccountId = findAccountReception.getSmallAccountId();
        String who = findAccountReception.getWho();
        if (Objects.equals(who, "")){
            who=null;
        }
        java.sql.Date startTime=null;
        java.sql.Date endTime=null;
        if (findAccountReception.getTimeZone()!=null){
            List<java.sql.Date> timeZone = findAccountReception.getTimeZone();
            startTime = timeZone.get(0);
            endTime = timeZone.get(1);
        }
        now=(now-1)*size;
        return accountMapper.selectAccountData(now,size,way,payOrTakeWay,bigAccountId,smallAccountId,who,startTime,endTime);
    }

    @Override
    public Integer selectAccountDataCount(FindAccountReception findAccountReception) {
        Integer way = findAccountReception.getWay();
        Integer payOrTakeWay = findAccountReception.getPayOrTakeWay();
        Integer bigAccountId = findAccountReception.getBigAccountId();
        Integer smallAccountId = findAccountReception.getSmallAccountId();
        String who = findAccountReception.getWho();
        if (Objects.equals(who, "")){
            who=null;
        }
        java.sql.Date startTime=null;
        java.sql.Date endTime=null;
        if (findAccountReception.getTimeZone()!=null){
            List<java.sql.Date> timeZone = findAccountReception.getTimeZone();
            startTime = timeZone.get(0);
            endTime = timeZone.get(1);
        }
        return accountMapper.selectAccountDataCount(way,payOrTakeWay,bigAccountId,smallAccountId,who,startTime,endTime);
    }

    @Override
    public Map<String, BigDecimal> showPayRevenueSumThisDay() {
        Map<String, BigDecimal> hashMaps = new HashMap<>();
        BigDecimal bigDecimalByPay = accountMapper.showPayRevenueSumThisDayByPay();
        if (bigDecimalByPay==null){
            bigDecimalByPay=new BigDecimal("0");
        }
        hashMaps.put("pay",bigDecimalByPay);
        BigDecimal bigDecimalByRevenus = accountMapper.showPayRevenueSumThisDayByRevenue();
        if (bigDecimalByRevenus==null){
            bigDecimalByRevenus=new BigDecimal("0");
        }
        hashMaps.put("revenue",bigDecimalByRevenus);

        return hashMaps;
    }
}
